package cn.bjxy.javaks.store_ms.service;

import java.text.ParseException;
import java.util.List;

import cn.bjxy.javaks.store_ms.entity.AdminEntity;
import cn.bjxy.javaks.store_ms.ui.MainFrame;
import cn.bjxy.javaks.store_ms.utils.StoreMSysUtil;

/**
 * 界面管理员信息管理业务处理代码
 * 
 * @author Administrator
 *
 */
public class UIAdminService {

	private static IAdminService adminService = AdminServiceImpl.getProxyInstance();

	/**
	 * 获得所有管理员信息数据
	 */
	public static void getAllAdmin() {

		/* 获得所有管理员实体 */
		List adminList = adminService.findAll();

		for (int i = 0; i < adminList.size(); i++) {
			MainFrame.addRow(createRowData((AdminEntity) adminList.get(i)));
		}
	}

	/**
	 * 判断管理员信息是否添加
	 * 
	 * @param rowData
	 *            管理员信息数据
	 * @return
	 * @throws ParseException
	 */
	public static boolean adminIsAdd() {
		AdminEntity adminEntity = createEntity(1);
		if (adminService.add(adminEntity) == 1) {
			MainFrame.addRow(createRowData(adminService.findById(adminEntity.getId())));
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断管理员信息是否删除
	 * 
	 * @param id
	 *            要删除的管理员的ID
	 * @return
	 */
	public static boolean adminIsDeleted(String id) {
		return (adminService.delete(id) == 1) ? true : false;
	}

	/**
	 * 判断管理员信息是否修改
	 * 
	 * @return
	 */
	public static boolean adminIsModify() {
		AdminEntity adminEntity = createEntity(0);
		if (adminService.modify(adminEntity) == 1) {
			MainFrame.modifyRow(createRowData(adminService.findById(adminEntity.getId())));
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 封装管理员实体
	 * 
	 * @flag 0 添加 需要获取新ID 1 修改 不需要新ID
	 */
	private static AdminEntity createEntity(int flag) {
		AdminEntity entity = new AdminEntity();
		String[] rowData = MainFrame.getData();

		if (flag == 1)
			entity.setId(StoreMSysUtil.generateShortUuid());
		else
			entity.setId(rowData[0]);
		entity.setName(rowData[1]);
		entity.setPassword(rowData[2]);
		entity.setSex(Boolean.valueOf(rowData[3]));
		entity.setLevel(Integer.valueOf(rowData[4]));

		return entity;
	}

	/**
	 * 封装表格管理员信息
	 * 
	 */
	private static String[] createRowData(AdminEntity adminEntity) {

		String id = adminEntity.getId();
		String name = adminEntity.getName();
		String password = adminEntity.getPassword();
		String gender = "";
		if (adminEntity.isSex()) {
			gender = "女";
		} else {
			gender = "男";
		}
		String level;
		if (adminEntity.getLevel() == 0) {
			level = "超级管理员";
		} else {
			level = "普通管理员";
		}
		String addDate = String.valueOf(adminEntity.getAdd_date());
		String modifyDate = String.valueOf(adminEntity.getModify_date());
		String[] rowData = { id, name, password, gender, level, addDate, modifyDate };

		return rowData;
	}
}
